
//
//  HOFillInfoViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 1/21/16.
//  Copyright © 2016 ShenOu. All rights reserved.
//

#import "HOFillInfoViewController.h"
#import "HOButton.h"
#import "HOUtils.h"
#import "Logger.h"
#import "HOUIMgr.h"
#import "HOUserInfoDataModule.h"
#import "HOConstants.h"
#import "HOTabViewController.h"
#import "HOEditView.h"
@interface HOFillInfoViewController()<UITextFieldDelegate, HOEditViewDelegate>
{
	HOEditView		*userNameInputBox;
	UIButton		*submitBtn;
}
@end
@implementation HOFillInfoViewController
@synthesize currentUserType;
- (void)viewDidLoad
{
	[super viewDidLoad];
	[self initTableView];
	[self initSubViews];
	baseTableview.scrollEnabled = NO;
	baseTableview.delaysContentTouches = NO;
}
- (void)initSubViews
{
	userNameInputBox = [[HOEditView alloc]
						initWithFrame:CGRectMake(40,
												 0,
												 [HOUtils getScreenWidth] - 80,
												 50)];
//
//	[userNameInputBox setFont:[UIFont boldSystemFontOfSize:15]];
//	userNameInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
//	[userNameInputBox setTextColor:[UIColor blackColor]];
//	userNameInputBox.keyboardType = UIKeyboardTypePhonePad;
//	userNameInputBox.returnKeyType = UIReturnKeyNext;
//	userNameInputBox.placeholder = NSLocalizedString(@"请输入手机号码", @"请输入手机号码");
//	userNameInputBox.delegate = self;
	
	userNameInputBox.limitedCount = 120;
	userNameInputBox.delegate = self;
	userNameInputBox.layer.cornerRadius = 0;
	userNameInputBox.layer.borderWidth = 0;
	[userNameInputBox.layer setMasksToBounds:YES];
	[userNameInputBox setPlaceHolder:NSLocalizedString(@"说点什么", @"说点什么")];
	[userNameInputBox getTextView].returnKeyType = UIReturnKeySend;

}

//- (void)createLiginBtn
//{
//	UIView *view = [[UIView alloc]
//					initWithFrame:CGRectMake(0,
//											 0,
//											 self.view.frame.size.width,
//											 100)];
//	submitBtn = [[UIButton alloc]initWithFrame:
//				CGRectMake(HO_HORIZONAL_PADDING,
//						   50,
//						   self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
//						   40)];
//	//    [submitBtn setStyleWithTitle:@"登录"
//	//                         titleColor:[UIColor whiteColor]
//	//                          titleFont:[UIFont systemFontOfSize:CELL_FONT_SIZE]
//	//                         norBgColor:MAIN_COLOR_NORMAL
//	//                         selBgColor:nil
//	//                    highlighBgColor:nil
//	//                           selector:@selector(login:)
//	//                             target:self];
//	[submitBtn.layer setMasksToBounds:YES];
//	[submitBtn setTitle:NSLocalizedString(@"确定", @"确定") forState:UIControlStateNormal];
//	[submitBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
//	[submitBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:submitBtn] forState:UIControlStateNormal];
//	[submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//	[submitBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_TOUCHED forView:submitBtn] forState:UIControlStateSelected];
//	[submitBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_TOUCHED forView:submitBtn] forState:UIControlStateHighlighted];
//	[submitBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_DISABLED forView:submitBtn] forState:UIControlStateDisabled];
//	submitBtn.enabled = NO;
//	submitBtn.layer.cornerRadius = 5;
//	[view addSubview:submitBtn];
//	baseTableview.tableFooterView = view;
//	
//	
//	
//}

- (UILabel *)getLblWithText:(NSString *)text
{
	UILabel *lbl = [[[UILabel alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  65,
											  22)] autorelease];
	lbl.textAlignment = NSTextAlignmentCenter;
	lbl.textColor = UIColorFromRGB(0x666000);
	lbl.text = text;
	[lbl setFont:[HOUtils fontWithSize:12]];
	return lbl;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[self rightButtonItemDidClick];
	return YES;
}



- (void)viewWillAppear:(BOOL)animated
{
	//    self.view.layer.borderWidth = 1;
	self.tabBarController.tabBar.hidden = YES;
	//    [self.tabBarController.tabBar setBackgroundColor:[UIColor redColor]];
	self.navigationController.navigationBar.hidden = NO;
	[self displayRightButtonItemWithTitle:NSLocalizedString(@"确定提交", @"确定提交")];
	[self setNavigationItemTitle:@"填写资料"];
	[self registerKeyboard];
	[self registerTapGesture];
	[userNameInputBox becomeFirstResponder];
	[super viewWillAppear:animated];
}

- (void)hideKeyboard
{
	[userNameInputBox resignFirstResponder];	
}
- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	[self unregisterTapGesture];
	self.noResultView.hidden = YES;
	
}

- (void)rightButtonItemDidClick
{
	if(userNameInputBox == nil)
	{
		return;
	}
	
	NSString *phoneNumber = [self getTrimingCharaFromTextField:[userNameInputBox getText]];
	if(phoneNumber.length == 0){
		[self popupHintView:NSLocalizedString(@"请输入手机号码", @"请输入手机号码")];
		return;
	}
	if(phoneNumber.length != 11){
		[self popupHintView:NSLocalizedString(@"请输入正确的手机号码", @"请输入正确的手机号码")];
		return;
	}
	
	
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_UpdateUserInfo,
										kHO_FIELD_Mobile:phoneNumber}];
}

- (void)didSelectNormalUser:(id)sender
{
	self.currentUserType = HO_USER_TYPE_LEADER;
	[baseTableview reloadData];
}

- (void)didSelectTranslateUser:(id)sender
{
	self.currentUserType = HO_USER_TYPE_LEADER;
	[baseTableview reloadData];
}


#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//	return 120;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	
	return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
	cell.accessoryType = UITableViewCellAccessoryNone;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	
	NSArray *views = [NSArray arrayWithArray:cell.contentView.subviews];
	for(UIView *view in views)
	{
		[view removeFromSuperview];
	}
	
	UIImage *img = nil;
	if(indexPath.row == 0)
		img = [UIImage imageNamed:@"icon_phone"];
	else
		img = [UIImage imageNamed:@"icon_lock"];
	UIImageView *icImgView = (UIImageView *)[cell viewWithTag:34];
	if(icImgView == nil)
	{
		icImgView = [[UIImageView alloc]
					 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											  (cellHeight - 18) / 2,
											  10,
											  18)];
		[cell.contentView addSubview:icImgView];
		icImgView.tag = 34;
		[icImgView release];
	}
	icImgView = (UIImageView *)[cell viewWithTag:34];
	icImgView.image = img;
	
	HOEditView * inputBox = (HOEditView *)[cell viewWithTag:2];
	if(inputBox == nil)
	{
		inputBox = userNameInputBox;
		inputBox.tag = 2;
		[cell.contentView addSubview:inputBox];
	}
	
	
	return cell;
}

#pragma mark

#pragma mark -- handle event.

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	[super requestDidSuccessWithResponse:response];
	if([response.action isEqualToString:kHO_ACTION_UpdateUserInfo])
	{
		HOTabViewController *viewCtrl = [[HOTabViewController alloc]init];
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}

#pragma mark
- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:userNameInputBox, submitBtn];
	[userNameInputBox release];
	[submitBtn release];
	[super dealloc];
	
}
@end
